'use strict';

const {Service} = require('ee-core');
const HttpClient = require('ee-core/httpclient');
const {AccountResp} = require("../model/AccountResp");
const {ConfigContext} = require("../context/ConfigContext");
const {PbContext} = require("../context/PbContext");
const {LockUtil} = require("../utils/LockUtil");
const {ElapsedTime} = require("../utils/ElapsedTimeUtil");
const {PbDataHandler} = require("../utils/handler/PbDataHandler");
const Log = require('ee-core/log');


/**
 * 示例服务（service层为单例）
 * @class
 */
class PbService extends Service {

    constructor(ctx) {
        super(ctx);
    }
    httpclient = new HttpClient();
    /**
     * 获取账号信息
     *
     * @return {Promise<AccountResp|null>}
     */
    async getAccount() {
        const url = `https://${ConfigContext.globConfig.pbHost}/member-service/v2/account-balance?locale=zh_CN`;
        let resp = await this.httpclient.curl(url, {
            method: 'POST',
            headers: PbContext.headerContext.header,
            dataType: 'json',
            timeout: 15000,
        });
        let {loginId, betCredit} = resp.data
        PbContext.refreshUserContext(loginId,loginId,betCredit)
        return resp.data;
    }


    /**
     * 获取正在进行的比赛
     * @returns {Promise<GameListResp>}
     */
    async getGameList() {
        // 上锁,避免连续点击多次刷新
        const lockKey = `game_list_lock_getGameList`;
        const b = LockUtil.lock(lockKey);
        if (!b) {
            throw new ReferenceError("Pb比赛列表查询正在处理中，请勿重复操作！");
        }
        try {
            const nowTime = Date.now();
            const sportId = 29;//29-足球
            // const version = PbDataHandler.getVersion(sportId);
            const version = 0;
            const url = `https://${ConfigContext.globConfig.pbHost}/sports-service/sv/compact/events?btg=1&c=&cl=100&d=&ec=&ev=&g=QQ%3D%3D&hle=false&inl=false&l=100&lang=&lg=&lv=&me=0&mk=2&more=false&o=1&ot=1&pa=0&pimo=0%2C1%2C8%2C39%2C2%2C3%2C6%2C7%2C4%2C5&pn=-1&pv=1&sp=${sportId}&tm=0&v=${version}&locale=zh_CN&_=${nowTime}&withCredentials=true`;
            const s = new ElapsedTime();
            let resp = await this.httpclient.curl(url, {
                method: 'GET',
                headers: PbContext.headerContext.header,
                dataType: 'json',
                timeout: 15000,
            });
            const e = s.stop();
            console.log('PG getGameList请求耗时：' + e + "ms");
            return PbDataHandler.parseGameList(resp.data, true);
        } finally {
            LockUtil.unLock(lockKey);
        }
    }


    /**
     * 获取近期的赛事
     * @returns {Promise<GameListResp>}
     */
    async getRecentGameList() {
        const nowTime = Date.now();
        const sportId = 29;//29-足球
        const version = 0;
        const url = `https://${ConfigContext.globConfig.pbHost}/sports-service/sv/compact/events?btg=1&c=&cl=1&d=&ec=&ev=&g=QQ%3D%3D&hle=false&inl=false&l=1&lang=&lg=&lv=&me=0&mk=1&more=false&o=1&ot=1&pa=0&pimo=0%2C1%2C8%2C39%2C2%2C3%2C6%2C7%2C4%2C5&pn=-1&pv=1&sp=${sportId}&tm=0&v=${version}&locale=zh_CN&_=${nowTime}&withCredentials=true`;
        const s = new ElapsedTime();
        let resp = await this.httpclient.curl(url, {
            method: 'GET',
            headers: PbContext.headerContext.header,
            dataType: 'json',
            timeout: 15000,
        });
        const e = s.stop();
        console.log('PG getPbRecentGameList请求耗时：' + e + "ms");
        return PbDataHandler.parseGameList(resp.data, false);
    }



}

PbService.toString = () => '[class PbService]';
module.exports = PbService;